package com.wl.handle;

import javax.servlet.http.HttpServletRequest;

import com.wl.domain.Result;
import com.wl.exception.GirlException;
import com.wl.utils.ResultUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author wl
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result<?> handle(Exception e) {
        if (e instanceof GirlException) {
            GirlException girlException = (GirlException) e;
            return ResultUtil.error(girlException.getCode(), girlException.getMessage());
        }else {
            logger.error("【系统异常】{}", e);
            return ResultUtil.error(-1, "未知错误");
        }
    }
    
      
//    @ExceptionHandler(value = Exception.class)  
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) {  
        ModelAndView mv = new ModelAndView();  
        mv.addObject("e", e);  
        mv.addObject("uri", req.getRequestURI());  
        return mv;  
    }  

}
